package panda.client.engine;

import java.io.Serializable;

import panda.client.enumerations.CardRarity;
import panda.client.enumerations.MagicSet;
import panda.client.enumerations.ZIndexLevel;

/**
 * Contains information for a unique Magic: The Gathering card. Immutable.
 * 
 * @author zane.tian@gmail.com
 */
public class MagicCard implements Serializable {
	private static final long serialVersionUID = 3418205940564362184L;
	
	MagicSet set;
	String name;
	String castingCost;
	String type;
	String illustrator;
	CardRarity rarity;
	String rulings;

	private String root = "http://kyruru.net/panda/scans/";
	
	private String url;
	private String tappedUrl;
	private String url50percent;
	private String url75percent;
	private String url100percent;
	private String flipped180Url;
	
	int zIndex;

	boolean tapped;

	/* Magic cards are numbered per set. For example, Kird Ape is #199 of ninth edition. */
	int id;

	/* Helps the server keep track of multiple copies of the same card. */
	long serverID = 0;

	/* The card's body text in HTML. */
	String bodyText;

	public MagicCard() {}

	public MagicCard(MagicSet set, String name, String castingCost, String type, String bodyText, CardRarity rarity,
			String illustrator, /* String rulings, */int id) {

		/* makes sure tokens are only created with MagicCardToken's constructor. */
		/* Note: this is bad because a superclass isn't supposed to know about its subclasses =( */
		if(set.getAbbrev().contains("token") && !(this instanceof MagicCardToken)) {
			throw new IllegalArgumentException("Cannot make a MagicCard with " + set.getAbbrev()
					+ ". Use MagicCardToken constructor instead");
		}

		this.set = set;
		this.name = name;
		this.castingCost = castingCost;
		this.type = type;
		this.bodyText = bodyText;
		this.rarity = rarity;
		this.illustrator = illustrator;
		this.rulings = "";
		this.id = id;
		// this.url = this.set.getAbbrev() + "/" + id + ".jpg";
		
		this.url = root + getMagicSet().getAbbrev() + "/25percent/" + getID() + ".jpg";
		this.tappedUrl = root + getMagicSet().getAbbrev() + "/25percent/tapped/" + getID() + ".jpg";
		this.url50percent = root + getMagicSet().getAbbrev() + "/50percent/" + getID() + ".jpg";
		this.url75percent = root + getMagicSet().getAbbrev() + "/75percent/" + getID() + ".jpg";
		// this.url75percent = "http://magiccards.info/scans/en/" + card.getMagicSet().getAbbrev() + "/" + card.getID() +
		// ".jpg";
		this.url100percent = root + getMagicSet().getAbbrev() + "/100percent/" + getID() + ".jpg";
		this.flipped180Url = root + getMagicSet().getAbbrev() + "/25percent/180degrees/" + getID() + ".jpg";

		tapped = false;
		zIndex = ZIndexLevel.MAGICCARD.getZIndex();
	}

	public int getZIndex() {
		return zIndex;
	}

	/**
	 * Does not allow the z-index to fall below 0;
	 * 
	 * @param index
	 */
	public void setZIndex(int index) {
		zIndex = index;
		if(zIndex < 1) {
			zIndex = 1;
		}
		
		if(zIndex > 44) {
			zIndex = 44;
		}
	}

	public boolean getTapped() {
		return tapped;
	}

	public void setTapped(boolean tapped) {
		this.tapped = tapped;
	}

	public void setServerID(long id) {
		this.serverID = id;
	}

	public long getServerID() {
		return serverID;
	}

	public MagicSet getMagicSet() {
		return set;
	}

	public String getName() {
		return name;
	}

	public String getNameAndServerID() {
		return name + " [" + serverID + "]";
	}

	public String getCastingCost() {
		return castingCost;
	}

	public String getType() {
		return type;
	}

	public String getIllustrator() {
		return illustrator;
	}

	public CardRarity getRarity() {
		return rarity;
	}

	public String getBodyText() {
		return bodyText;
	}

	/*
	 * public String getRulings() { return rulings; }
	 */

	public int getID() {
		return id;
	}
	
	public String toString() {
		return name;
	}
	
	public String getTapUrl() {
		return tappedUrl;
	}

	public String getImgUrl() {
		return url;
	}
	
	public String getUrl75() {
		return url75percent;
	}

	public String getZoomUrl() {
		return url100percent;
		// return url75percent;
	}

	public String getUrl100() {
		return url100percent;
	}

	public String getUrl50() {
		return url50percent;
	}

	public String get180Url() {
		return flipped180Url;
	}
}